<?php
/**
 * Created by PhpStorm.
 * User: bolle
 * Date: 26.12.15
 * Time: 10:11
 */

namespace tbollmeier\alteraparser\syntaxgraph;


class Branches extends VertexGroup
{
    private $branches;

    public function __construct()
    {
        parent::__construct();
        $this->branches = [];
    }

    public function addBranch($branch)
    {
        array_push($this->branches, self::copyBranch($branch));
    }

    protected function onExpand(&$start, &$end)
    {
        foreach ($this->branches as $branch) {
            $current = $start;
            foreach ($branch as $element) {
                $current = $current->connect($element);
            }
            $current->connect($end);
        }
    }

    protected function onCopy($original)
    {
        parent::onCopy($original);

        $this->branches = [];
        foreach ($original->branches as $branch) {
            array_push($this->branches, self::copyBranch($branch));
        }

    }

    private static function copyBranch($branch)
    {
        $copiedBranch = [];
        foreach ($branch as $element) {
            array_push($copiedBranch, $element->copy());
        }

        return $copiedBranch;
    }

}
